 package tools.wztosql;

 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import java.io.File;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.util.LinkedList;
 import java.util.List;
 import provider.MapleData;
 import provider.MapleDataProvider;
 import provider.MapleDataProviderFactory;
 import provider.MapleDataTool;
 import server.quest.MapleQuestActionType;
 import server.quest.MapleQuestRequirementType;
 import tools.Pair;

 public class DumpQuests
 {
/*  20 */   private MapleDataProvider quest = MapleDataProviderFactory.getDataProvider(new File(System.getProperty("wzpath") + "/Quest.wz"));
   protected boolean hadError = false;
/*  22 */   protected int id = 0;

   public void dumpQuests() throws Exception {
/*  25 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  26 */       PreparedStatement psai = con.prepareStatement("INSERT INTO wz_questactitemdata(uniqueid, itemid, count, period, gender, job, jobEx, prop) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
/*  27 */       PreparedStatement psas = con.prepareStatement("INSERT INTO wz_questactskilldata(uniqueid, skillid, skillLevel, masterLevel) VALUES (?, ?, ?, ?)");
/*  28 */       PreparedStatement psaq = con.prepareStatement("INSERT INTO wz_questactquestdata(uniqueid, quest, state) VALUES (?, ?, ?)");
/*  29 */       PreparedStatement ps = con.prepareStatement("INSERT INTO wz_questdata(questid, name, autoStart, autoPreComplete, viewMedalItem, selectedSkillID, blocked, autoAccept, autoComplete,selfStart) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
/*  30 */       PreparedStatement psr = con.prepareStatement("INSERT INTO wz_questreqdata(questid, type, name, stringStore, intStoresFirst, intStoresSecond) VALUES (?, ?, ?, ?, ?, ?)");
/*  31 */       PreparedStatement psq = con.prepareStatement("INSERT INTO wz_questpartydata(questid, rank, mode, property, value) VALUES(?,?,?,?,?)");
/*  32 */       PreparedStatement psa = con.prepareStatement("INSERT INTO wz_questactdata(questid, type, name, intStore, applicableJobs, uniqueid) VALUES (?, ?, ?, ?, ?, ?)");
       try {
/*  34 */         dumpQuests(psai, psas, psaq, ps, psr, psq, psa);
/*  35 */       } catch (Exception e) {
/*  36 */         System.out.println(this.id + " quest.");
/*  37 */         e.printStackTrace();
/*  38 */         this.hadError = true;
       } finally {
/*  40 */         psai.executeBatch();
/*  41 */         psai.close();
/*  42 */         psas.executeBatch();
/*  43 */         psas.close();
/*  44 */         psaq.executeBatch();
/*  45 */         psaq.close();
/*  46 */         psa.executeBatch();
/*  47 */         psa.close();
/*  48 */         psr.executeBatch();
/*  49 */         psr.close();
/*  50 */         psq.executeBatch();
/*  51 */         psq.close();
/*  52 */         ps.executeBatch();
/*  53 */         ps.close();
       }
     }
   }

   public void delete(String sql) throws Exception {
/*  59 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  60 */       PreparedStatement ps = con.prepareStatement(sql);
/*  61 */       ps.executeUpdate();
/*  62 */       ps.close();
     }
   }


   public boolean doesExist(String sql) throws Exception {
/*  68 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {
/*  69 */       PreparedStatement ps = con.prepareStatement(sql);
/*  70 */       ResultSet rs = ps.executeQuery();
/*  71 */       boolean ret = rs.next();
/*  72 */       rs.close();
/*  73 */       ps.close();
/*  74 */       return ret;
     }
   }


   public void dumpQuests(PreparedStatement psai, PreparedStatement psas, PreparedStatement psaq, PreparedStatement ps, PreparedStatement psr, PreparedStatement psq, PreparedStatement psa) throws Exception {
/*  80 */     delete("DELETE FROM wz_questdata");
/*  81 */     delete("DELETE FROM wz_questactdata");
/*  82 */     delete("DELETE FROM wz_questactitemdata");
/*  83 */     delete("DELETE FROM wz_questactskilldata");
/*  84 */     delete("DELETE FROM wz_questactquestdata");
/*  85 */     delete("DELETE FROM wz_questreqdata");
/*  86 */     delete("DELETE FROM wz_questpartydata");
/*  87 */     System.out.println("Deleted wz_questdata successfully.");
/*  88 */     MapleData checkz = this.quest.getData("Check.img");
/*  89 */     MapleData actz = this.quest.getData("Act.img");
/*  90 */     MapleData infoz = this.quest.getData("QuestInfo.img");
/*  91 */     MapleData pinfoz = this.quest.getData("PQuest.img");
/*  92 */     System.out.println("Adding into wz_questdata.....");
/*  93 */     int uniqueid = 0;
/*  94 */     for (MapleData qz : checkz.getChildren()) {
/*  95 */       this.id = Integer.parseInt(qz.getName());
/*  96 */       ps.setInt(1, this.id);
/*  97 */       for (int i = 0; i < 2; i++) {
/*  98 */         MapleData reqData = qz.getChildByPath(String.valueOf(i));
/*  99 */         if (reqData != null) {
/* 100 */           psr.setInt(1, this.id);
/* 101 */           psr.setInt(2, i);
/* 102 */           for (MapleData req : reqData.getChildren()) {
/* 103 */             if (MapleQuestRequirementType.getByWZName(req.getName()) == MapleQuestRequirementType.UNDEFINED) {
               continue;
             }
/* 106 */             psr.setString(3, req.getName());
/* 107 */             if (req.getName().equals("fieldEnter")) {
/* 108 */               psr.setString(4, String.valueOf(MapleDataTool.getIntConvert("0", req, 0)));
/* 109 */             } else if (req.getName().equals("end") || req.getName().equals("startscript") || req.getName().equals("endscript")) {
/* 110 */               psr.setString(4, MapleDataTool.getString(req, ""));
             } else {
/* 112 */               psr.setString(4, String.valueOf(MapleDataTool.getInt(req, 0)));
             }
/* 114 */             StringBuilder intStore1 = new StringBuilder();
/* 115 */             StringBuilder intStore2 = new StringBuilder();
/* 116 */             List<Pair<Integer, Integer>> dataStore = new LinkedList<>();
/* 117 */             if (req.getName().equals("job")) {
/* 118 */               List<MapleData> child = req.getChildren();
/* 119 */               for (int x = 0; x < child.size(); x++) {
/* 120 */                 dataStore.add(new Pair(Integer.valueOf(i), Integer.valueOf(MapleDataTool.getInt(child.get(x), -1))));
               }
/* 122 */             } else if (req.getName().equals("skill")) {
/* 123 */               List<MapleData> child = req.getChildren();
/* 124 */               for (int x = 0; x < child.size(); x++) {
/* 125 */                 MapleData childdata = child.get(x);
/* 126 */                 if (childdata != null)
                 {

/* 129 */                   dataStore.add(new Pair(Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("id"), 0)), 
/* 130 */                         Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("acquire"), 0)))); } 
               }
/* 132 */             } else if (req.getName().equals("quest")) {
/* 133 */               List<MapleData> child = req.getChildren();
/* 134 */               for (int x = 0; x < child.size(); x++) {
/* 135 */                 MapleData childdata = child.get(x);
/* 136 */                 if (childdata != null)
                 {

/* 139 */                   dataStore.add(new Pair(Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("id"), 0)), 
/* 140 */                         Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("state"), 0)))); } 
               }
/* 142 */             } else if (req.getName().equals("item") || req.getName().equals("mob")) {
/* 143 */               List<MapleData> child = req.getChildren();
/* 144 */               for (int x = 0; x < child.size(); x++) {
/* 145 */                 MapleData childdata = child.get(x);
/* 146 */                 if (childdata != null)
                 {

/* 149 */                   dataStore.add(new Pair(Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("id"), 0)), 
/* 150 */                         Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("count"), 0)))); } 
               }
/* 152 */             } else if (req.getName().equals("mbcard")) {
/* 153 */               List<MapleData> child = req.getChildren();
/* 154 */               for (int x = 0; x < child.size(); x++) {
/* 155 */                 MapleData childdata = child.get(x);
/* 156 */                 if (childdata != null)
                 {

/* 159 */                   dataStore.add(new Pair(Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("id"), 0)), 
/* 160 */                         Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("min"), 0)))); } 
               }
/* 162 */             } else if (req.getName().equals("pet")) {
/* 163 */               List<MapleData> child = req.getChildren();
/* 164 */               for (int x = 0; x < child.size(); x++) {
/* 165 */                 MapleData childdata = child.get(x);
/* 166 */                 if (childdata != null)
                 {

/* 169 */                   dataStore.add(new Pair(Integer.valueOf(i), 
/* 170 */                         Integer.valueOf(MapleDataTool.getInt(childdata.getChildByPath("id"), 0)))); } 
               }
             }
/* 173 */             for (Pair<Integer, Integer> data : dataStore) {
/* 174 */               if (intStore1.length() > 0) {
/* 175 */                 intStore1.append(", ");
/* 176 */                 intStore2.append(", ");
               }
/* 178 */               intStore1.append(data.getLeft());
/* 179 */               intStore2.append(data.getRight());
             }
/* 181 */             psr.setString(5, intStore1.toString());
/* 182 */             psr.setString(6, intStore2.toString());
/* 183 */             psr.addBatch();
           }
         }
/* 186 */         MapleData actData = actz.getChildByPath(this.id + "/" + i);
/* 187 */         if (actData != null) {
/* 188 */           psa.setInt(1, this.id);
/* 189 */           psa.setInt(2, i);
/* 190 */           for (MapleData act : actData.getChildren()) {
/* 191 */             if (MapleQuestActionType.getByWZName(act.getName()) == MapleQuestActionType.UNDEFINED) {
               continue;
             }
/* 194 */             psa.setString(3, act.getName());
/* 195 */             if (act.getName().equals("sp")) {
/* 196 */               psa.setInt(4, MapleDataTool.getIntConvert("0/sp_value", act, 0));
             } else {
/* 198 */               psa.setInt(4, MapleDataTool.getInt(act, 0));
             }
/* 200 */             StringBuilder applicableJobs = new StringBuilder();
/* 201 */             if (act.getName().equals("sp") || act.getName().equals("skill")) {
/* 202 */               int index = 0;

/* 204 */               while (act.getChildByPath(index + "/job") != null) {
/* 205 */                 for (MapleData d : act.getChildByPath(index + "/job")) {
/* 206 */                   if (applicableJobs.length() > 0) {
/* 207 */                     applicableJobs.append(", ");
                   }
/* 209 */                   applicableJobs.append(MapleDataTool.getInt(d, 0));
                 }
/* 211 */                 index++;

               }


             }
/* 217 */             else if (act.getChildByPath("job") != null) {
/* 218 */               for (MapleData d : act.getChildByPath("job")) {
/* 219 */                 if (applicableJobs.length() > 0) {
/* 220 */                   applicableJobs.append(", ");
                 }
/* 222 */                 applicableJobs.append(MapleDataTool.getInt(d, 0));
               }
             }

/* 226 */             psa.setString(5, applicableJobs.toString());
/* 227 */             psa.setInt(6, -1);
/* 228 */             if (act.getName().equals("item")) {
/* 229 */               uniqueid++;
/* 230 */               psa.setInt(6, uniqueid);
/* 231 */               psai.setInt(1, uniqueid);
/* 232 */               for (MapleData iEntry : act.getChildren()) {
/* 233 */                 psai.setInt(2, MapleDataTool.getInt("id", iEntry, 0));
/* 234 */                 psai.setInt(3, MapleDataTool.getInt("count", iEntry, 0));
/* 235 */                 psai.setInt(4, MapleDataTool.getInt("period", iEntry, 0));
/* 236 */                 psai.setInt(5, MapleDataTool.getInt("gender", iEntry, 2));
/* 237 */                 psai.setInt(6, MapleDataTool.getInt("job", iEntry, -1));
/* 238 */                 psai.setInt(7, MapleDataTool.getInt("jobEx", iEntry, -1));
/* 239 */                 if (iEntry.getChildByPath("prop") == null) {
/* 240 */                   psai.setInt(8, -2);
                 } else {
/* 242 */                   psai.setInt(8, MapleDataTool.getInt("prop", iEntry, -1));
                 }
/* 244 */                 psai.addBatch();
               }
/* 246 */             } else if (act.getName().equals("skill")) {
/* 247 */               uniqueid++;
/* 248 */               psa.setInt(6, uniqueid);
/* 249 */               psas.setInt(1, uniqueid);
/* 250 */               for (MapleData sEntry : act) {
/* 251 */                 psas.setInt(2, MapleDataTool.getInt("id", sEntry, 0));
/* 252 */                 psas.setInt(3, MapleDataTool.getInt("skillLevel", sEntry, 0));
/* 253 */                 psas.setInt(4, MapleDataTool.getInt("masterLevel", sEntry, 0));
/* 254 */                 psas.addBatch();
               }
/* 256 */             } else if (act.getName().equals("quest")) {
/* 257 */               uniqueid++;
/* 258 */               psa.setInt(6, uniqueid);
/* 259 */               psaq.setInt(1, uniqueid);
/* 260 */               for (MapleData sEntry : act) {
/* 261 */                 psaq.setInt(2, MapleDataTool.getInt("id", sEntry, 0));
/* 262 */                 psaq.setInt(3, MapleDataTool.getInt("state", sEntry, 0));
/* 263 */                 psaq.addBatch();
               }
             }
/* 266 */             psa.addBatch();
           }
         }
       }
/* 270 */       MapleData infoData = infoz.getChildByPath(String.valueOf(this.id));
/* 271 */       if (infoData != null) {
/* 272 */         ps.setString(2, MapleDataTool.getString("name", infoData, ""));
/* 273 */         ps.setInt(3, (MapleDataTool.getInt("autoStart", infoData, 0) > 0) ? 1 : 0);
/* 274 */         ps.setInt(4, (MapleDataTool.getInt("autoPreComplete", infoData, 0) > 0) ? 1 : 0);
/* 275 */         ps.setInt(5, MapleDataTool.getInt("viewMedalItem", infoData, 0));
/* 276 */         ps.setInt(6, MapleDataTool.getInt("selectedSkillID", infoData, 0));
/* 277 */         ps.setInt(7, MapleDataTool.getInt("blocked", infoData, 0));
/* 278 */         ps.setInt(8, MapleDataTool.getInt("autoAccept", infoData, 0));
/* 279 */         ps.setInt(9, MapleDataTool.getInt("autoComplete", infoData, 0));
/* 280 */         ps.setInt(10, MapleDataTool.getInt("selfStart", infoData, 0));
       } else {
/* 282 */         ps.setString(2, "");
/* 283 */         ps.setInt(3, 0);
/* 284 */         ps.setInt(4, 0);
/* 285 */         ps.setInt(5, 0);
/* 286 */         ps.setInt(6, 0);
/* 287 */         ps.setInt(7, 0);
/* 288 */         ps.setInt(8, 0);
/* 289 */         ps.setInt(9, 0);
/* 290 */         ps.setInt(10, 0);
       }
/* 292 */       ps.addBatch();

/* 294 */       MapleData pinfoData = pinfoz.getChildByPath(String.valueOf(this.id));
/* 295 */       if (pinfoData != null && pinfoData.getChildByPath("rank") != null) {
/* 296 */         psq.setInt(1, this.id);
/* 297 */         for (MapleData d : pinfoData.getChildByPath("rank")) {
/* 298 */           psq.setString(2, d.getName());
/* 299 */           for (MapleData c : d) {
/* 300 */             psq.setString(3, c.getName());
/* 301 */             for (MapleData b : c) {
/* 302 */               psq.setString(4, b.getName());
/* 303 */               psq.setInt(5, MapleDataTool.getInt(b, 0));
/* 304 */               psq.addBatch();
             }
           }
         }
       }
/* 309 */       System.out.println("Added quest: " + this.id);
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\tools\wztosql\DumpQuests.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */